<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <title>手风琴</title>
  <style>
    ul {
      list-style: none;
    }

    * {
      margin: 0;
      padding: 0;
    }

    div {
      width: 1200px;
      height: 400px;
      margin: 50px auto;
      border: 1px solid red;
      overflow: hidden;
    }

    div li {
      width: 240px;
      height: 400px;
      float: left;
      transition: all 500ms;
    }

    div ul {
      width: 1200px;
    }
  </style>
</head>

<body>
  <div id="box">
    <ul>
      <li>
        <a href="#">
          <img src="./images/1.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="#">
          <img src="./images/2.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="#">
          <img src="./images/3.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="#">
          <img src="./images/4.jpg" alt="">
        </a>
      </li>
      <li>
        <a href="#">
          <img src="./images/5.jpg" alt="">
        </a>
      </li>
    </ul>
  </div>
</body>
<script>
  let li = document.querySelectorAll('li')
  // 防抖第一步：声明变量储存定时器id
  let timeID = null
  for (let i = 0; i < li.length; i++) {
    //鼠标经过
    li[i].addEventListener('mouseenter', function () {
      //防抖第三步:每次触发之前都先停止之前的定时器
      clearTimeout(timeID)
      //防抖第二步:声明一个延时函数
      timeID = setTimeout(function () {
        for (let j = 0; j < li.length; j++) {
          li[j].style.width = '100px'
        }
        li[i].style.width = '800px'

        //鼠标离开
        li[i].addEventListener('mouseleave', function () {
          for (let i = 0; i < li.length; i++) {
            li[i].style.width = '240px'
          }
        })
      }, 300)
    })

  }

</script>

</html>